package com.example.home.attorney;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.attorney.activity.ConsultDetail;
import com.example.home.attorney.adapter.AttorneyCommentAdapter;
import com.example.home.attorney.bean.AttorneyCommentBean;
import com.example.home.attorney.bean.HotAttorneyBean;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;


public class UserEvaluateFragment extends Fragment {

    private HotAttorneyBean.DataBean hotAttId;
    private RecyclerView rcvList;
    private Integer id;
    private View view;
    private Button btn_zixun;

    public UserEvaluateFragment(HotAttorneyBean.DataBean rows) {
        this.hotAttId = rows;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_user_evaluate, container, false);
        id = hotAttId.getId();

        rcvList = view.findViewById(R.id.rcv_list);
        btn_zixun = view.findViewById(R.id.btn_zixun);


        btn_zixun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在按钮点击事件中启动 ConsultDetail 活动
                Intent intent = new Intent(getActivity(), ConsultDetail.class);
                Gson gson = new Gson();
                String serializedObject = gson.toJson(hotAttId);
                intent.putExtra("consull", serializedObject);
                startActivity(intent);
            }
        });


        getCommentData();

        return view;
    }

    private void getCommentData() {
        Log.e("xhf","123");
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/lawyer/list-evaluate?lawyerId="+id,
                OKHttpTool.GET,
                AttorneyCommentBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        AttorneyCommentBean attorneyCommentBean = (AttorneyCommentBean) obj;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.e("xhf","213");
                                bindCommentData(attorneyCommentBean);
                            }
                        });

                    }

                    @Override
                    public void onFailure(String msg) {
                        // 处理请求失败的情况
                    }
                }
        );
    }

    private void bindCommentData(AttorneyCommentBean attorneyCommentBean) {
        AttorneyCommentAdapter attorneyCommentAdapter = new AttorneyCommentAdapter(getActivity(),attorneyCommentBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        rcvList.setAdapter(attorneyCommentAdapter);
        rcvList.setLayoutManager(linearLayoutManager);
    }
}